package cn.com.surker.wechat;

import cn.com.surker.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Date;

/**
 * @Author lucky
 * @create 2023年10月26 15:05
 * @desc
 */
public class WechatControlUtils {

    /**
     * 文本框/文本域/标题空间
     * @param jo
     * @return
     */
    public static String textValue(JSONObject jo) {
        if (jo == null)
            return "";
        if (StringUtils.isNull(jo.getString("text")))
            return "";
        return jo.getString("text");
    }

    /**
     * 数字控件
     * @param jo
     * @return
     */
    public static String numberValue(JSONObject jo) {
        if (jo == null)
            return "";
        if (StringUtils.isNull(jo.getString("new_number")))
            return "";
        return jo.getString("new_number");
    }

    /**
     * 金额控件
     * @param jo
     * @return
     */
    public static String moneyValue(JSONObject jo) {
        if (jo == null)
            return "";
        if (StringUtils.isNull(jo.getString("new_money")))
            return "";
        return jo.getString("new_money");
    }

    /**
     * 时间控件
     * @param jo
     * @return
     */
    public static Date dateValue(JSONObject jo) {
        if (jo == null)
            return new Date();
        if (StringUtils.isNull(jo.getString("date")))
            return new Date();
        Long time = jo.getJSONObject("date").getLong("s_timestamp");
        if (StringUtils.isNull(time) || time < 1000)
            return new Date();

        return new Date(time * 1000);
    }

    /**
     * 单选/多选控件
     * @param jo
     * @return
     */
    public static String selectorValue(JSONObject jo) {
        String result = "";
        if (jo == null)
            return result;
        if (StringUtils.isNull(jo.getString("selector")))
            return result;
        JSONObject selector = jo.getJSONObject("selector");
        String type = selector.getString("type");
        if (StringUtils.isNull(type))
            return result;
        JSONArray options = selector.getJSONArray("options");
        if (options == null || options.size() == 0)
            return result;
        for (int i = 0; i < options.size(); i++) {
            JSONObject jsonObject = options.getJSONObject(i);
            JSONArray value = jsonObject.getJSONArray("value");
            if (value == null || value.size() == 0)
                continue;
            for (int j = 0; j < value.size(); j++) {
                String text = value.getJSONObject(j).getString("text");
                if (StringUtils.isNull(text))
                    continue;
                result += text + ",";
            }

        }
        if (result.length() > 0)
            result = result.substring(0, result.length() - 1);
        return result;
    }

    /**
     * 成员控件
     * @param jo
     * @return
     */
    public static String membersValue(JSONObject jo) {
        String result = "";
        if (jo == null)
            return result;
        if (StringUtils.isNull(jo.getString("members")))
            return result;
        JSONArray members = jo.getJSONArray("members");
        if (members == null || members.size() == 0)
            return result;
        for (int i = 0; i < members.size(); i++) {
            JSONObject jsonObject = members.getJSONObject(i);
            String name = jsonObject.getString("value");
            if (StringUtils.isNull(name))
                continue;
            result += name + ",";
        }
        if (result.length() > 0)
            result = result.substring(0, result.length() - 1);
        return result;
    }

    /**
     * 部门控件
     * @param jo
     * @return
     */
    public static String departmentsValue(JSONObject jo) {
        String result = "";
        if (jo == null)
            return result;
        if (StringUtils.isNull(jo.getString("departments")))
            return result;
        JSONArray departments = jo.getJSONArray("departments");
        if (departments == null || departments.size() == 0)
            return result;
        for (int i = 0; i < departments.size(); i++) {
            JSONObject jsonObject = departments.getJSONObject(i);
            String name = jsonObject.getString("name");
            if (!StringUtils.isNull(jsonObject.getString("value")))
                name = jsonObject.getString("value");
            if (StringUtils.isNull(name))
                continue;
            result += name + ",";
        }
        if (result.length() > 0)
            result = result.substring(0, result.length() - 1);
        return result;
    }

    /**
     * 附件控件
     * @param jo
     * @return
     */
    public static String fileValue(JSONObject jo) {
        String result = "";
        if (jo == null)
            return result;
        if (StringUtils.isNull(jo.getString("files")))
            return result;
        JSONArray files = jo.getJSONArray("files");
        if (files == null || files.size() == 0)
            return result;
        for (int i = 0; i < files.size(); i++) {
            JSONObject jsonObject = files.getJSONObject(i);
            String fileId = jsonObject.getString("file_id");
            if (StringUtils.isNull(fileId))
                continue;
            result += fileId + ",";
        }
        if (result.length() > 0)
            result = result.substring(0, result.length() - 1);
        return result;
    }
}
